package com.qo.qsdk.ui.base;

import android.app.Activity;
import android.graphics.Color;
import android.text.SpannableString;
import android.text.TextPaint;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.ForegroundColorSpan;
import android.view.View;
import android.widget.Toast;

import androidx.annotation.NonNull;

import com.qo.qsdk.model.SDKInfoModel;
import com.qo.qsdk.model.UserModel;
import com.qo.qsdk.ui.QSDKListener;
import com.qo.qsdk.utils.DynamicUtils;
import com.qo.qsdk.utils.Language;
import com.qo.qsdk.utils.QSDKHelper;

import static android.text.Spanned.SPAN_INCLUSIVE_EXCLUSIVE;

import com.qo.qsdk.ui.base.third.Px7TextView;
import com.qo.qsdk.ui.base.third.Px9TextView;
import com.qo.qsdk.ui.base.third.Px9EditText;
import com.qo.qsdk.ui.base.third.StrokeTextView;
import com.qo.qsdk.ui.base.third.StrokeTextViewY;
public class LoginInitView implements IBaseView{

    private View mAgree, mGoogle, mFb, mApple, mGuest, mAccount;
    private Px9TextView mDesc, mTitle,mGoogleText, mFbText, mAppleText, mGuestText, mAccountText,mChange;

    private View mMain;
    private Activity mActivity;

    private QSDKListener mIAccountListener;

    private boolean isAgree;

    public LoginInitView(Activity mActivity, QSDKListener iAccountListener) {
        this.mIAccountListener = iAccountListener;
        this.mActivity = mActivity;
        init(mActivity);
    }

    private UserModel mUserModel;
    public void setUserModel(UserModel m) {
        mUserModel = m;
    }

    private void init(Activity v) {
        mMain = v.findViewById(DynamicUtils.id(mActivity, "mainLoginInit"));
        mMain.setVisibility(View.GONE);

        mTitle = v.findViewById(DynamicUtils.id(mActivity, "loginInitTitle"));

        mGoogleText = v.findViewById(DynamicUtils.id(mActivity, "loginInitGoogleR"));
        mFbText = v.findViewById(DynamicUtils.id(mActivity, "loginInitFbR"));
        mAppleText = v.findViewById(DynamicUtils.id(mActivity, "loginInitAppleR"));
        mGuestText = v.findViewById(DynamicUtils.id(mActivity, "loginInitGuestR"));
        mAccountText = v.findViewById(DynamicUtils.id(mActivity, "loginInitAR"));

        v.findViewById(DynamicUtils.id(mActivity, "loginInitGoogle")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null) mIAccountListener.onLoginInitGoogle();
            }
        });
        v.findViewById(DynamicUtils.id(mActivity, "loginInitFb")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null) mIAccountListener.onLoginInitFb();
            }
        });
        v.findViewById(DynamicUtils.id(mActivity, "loginInitApple")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null) mIAccountListener.onLoginInitApple();
            }
        });
        v.findViewById(DynamicUtils.id(mActivity, "loginInitA")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null) mIAccountListener.onLoginInitAccount();
            }
        });
        v.findViewById(DynamicUtils.id(mActivity, "loginInitGuest")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (!isAgree) {
                    Toast.makeText(v, Language.Rule_Agree.toString() + "", Toast.LENGTH_SHORT).show();
                    return;
                }
                if (mIAccountListener != null) mIAccountListener.onLoginInitGuest();
            }
        });

        v.findViewById(DynamicUtils.id(mActivity, "loginInitChangeP")).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v2) {
                if (mIAccountListener != null) mIAccountListener.onChangeLanguage();
            }
        });

        mChange = v.findViewById(DynamicUtils.id(mActivity, "loginInitChange"));
        mDesc = v.findViewById(DynamicUtils.id(mActivity, "loginInitDesc"));
        mAgree = v.findViewById(DynamicUtils.id(mActivity, "loginInitAgree"));
        mAgree.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                v.setActivated(!v.isActivated());
                isAgree = v.isActivated();
            }
        });

        mMain.setVisibility(View.GONE);

        isAgree = true;
        mAgree.setActivated(isAgree);

        refresh();
    }

    public void show(Activity activity) {
        mMain.setVisibility(View.VISIBLE);
    }

    public void hide(Activity activity) {
        mMain.setVisibility(View.GONE);
    }

    public void refresh() {
        UserModel userModel = QSDKHelper.getInstance().getUserInfoModel(this.mActivity);
        SDKInfoModel config = QSDKHelper.getInstance().getConfigureModel();

        mChange.setText(Language.Login_Change_Lang_Vn.getDisplayLang());

        mTitle.setText(Language.Login_Init_Title.toString());
        mGoogleText.setText(Language.Login_Init_Google.toString());
        mFbText.setText(Language.Login_Init_Facebook.toString());
        mAppleText.setText(Language.Login_Init_Apple.toString());
        mAccountText.setText(Language.Login_Init_Account.toString());
        mGuestText.setText(Language.Login_Init_Guest.toString());

        String a = Language.Bind_Account_Desc_Pro.toString();
        String b = Language.Bind_Account_Desc_Pro_1.toString();
        String c = Language.Bind_Account_Desc_Pro_2.toString();
        String d = Language.Bind_Account_Desc_Pro_3.toString();
        SpannableString spannableString = new SpannableString(a);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#727785")),0, a.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#E4B575")),a.indexOf(b), a.indexOf(b) + b.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (mIAccountListener != null) {
                    mIAccountListener.onTerms();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.parseColor("#ED6E3D"));
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        },a.indexOf(b), a.indexOf(b) + b.length(), SPAN_INCLUSIVE_EXCLUSIVE);

        spannableString.setSpan(new ForegroundColorSpan(Color.parseColor("#E4B575")),  a.indexOf(d), a.indexOf(d) + d.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        spannableString.setSpan(new ClickableSpan() {
            @Override
            public void onClick(@NonNull View widget) {
                if (mIAccountListener != null) {
                    mIAccountListener.onProtocol();
                }
            }

            @Override
            public void updateDrawState(@NonNull TextPaint ds) {
                ds.setColor(Color.parseColor("#ED6E3D"));
                ds.setUnderlineText(false);
                ds.clearShadowLayer();
            }
        }, a.indexOf(d), a.indexOf(d) + d.length(), SPAN_INCLUSIVE_EXCLUSIVE);
        mDesc.setMovementMethod(LinkMovementMethod.getInstance());
        mDesc.setText(spannableString);
        mDesc.setHighlightColor(Color.TRANSPARENT);
    }


}
